package com.example.smartcity_poverty.ui.AllService.SmartBus.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.SmartBus.BusPersonListModel
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.SmartBus.adapter.PersonBusAdapter
import kotlinx.android.synthetic.main.fragment_bus1.*
import kotlinx.coroutines.Dispatchers


/**
 * 已支付页面
 */
class BusFragment1 : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bus1, container, false)
    }



    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 获取控件

        runThread()
    }

    private fun runThread() = coroutine(Dispatchers.IO){
        setList()
    }

    private suspend fun setList(){
        // 获取巴士订单列表
        val arrayList = ArrayList<BusPersonListModel.RowsBean>()
        coroutine(Dispatchers.IO){
            val result = api.getBusPersonList(getToken(), personId)
            if (result.code == 200){
                activity?.runOnUiThread {
                    // 设置适配器
                    arrayList.addAll(result.rows!!)

                    busFragmentListRv.layoutManager = LinearLayoutManager(context)
                    busFragmentListRv.adapter =
                        PersonBusAdapter(this@BusFragment1, arrayList, R.layout.re_bus_person_list)
                    busFragmentListRv.adapter?.notifyItemChanged(arrayList.size)
                }
            }else activity?.runOnUiThread { result.msg?.show() }
        }
    }

    companion object{
        var personId = 0
    }
}